﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

//namespace Assets.Script.Event
//{
    public static class EventControler
    {
        public static readonly Event Instance = null;

        class EventInfo
        {
            public object key;
            public Delegate handler;
        }

        private static Dictionary<object, List<EventInfo>> EventInfoDict = new Dictionary<object, List<EventInfo>>();
        // private static Dictionary<GameObject, List<EventInfo>> EventInfoDict = new Dictionary<GameObject, List<EventInfo>>();

        static EventControler()
        {
            if(Instance == null)
            {
                Instance = new Event();
            }
        }


    public static void AddListener(this object obj, object key, Action handler)
    {
        Instance.AddListener(key, handler);
        AddDict(obj, key, handler);
    }

    public static void AddListener<T>(this object obj, object key, Action<T> handler)
    {
        Instance.AddListener(key, handler);
        AddDict(obj, key, handler);
    }

    public static void AddListener<T, U>(this object obj, object key, Action<T, U> handler)
    {
        Instance.AddListener(key, handler);
        AddDict(obj, key, handler);
    }

    public static void AddListener<T, U, V>(this object obj, object key, Action<T, U, V> handler)
    {
        Instance.AddListener(key, handler);
        AddDict(obj, key, handler);
    }

    public static void AddListener<T, U, V, W>(this object obj, object key, Action<T, U, V, W> handler)
    {
        Instance.AddListener(key, handler);
        AddDict(obj, key, handler);
    }

    private static void AddDict(object obj, object key, Delegate handler)
    {
        EventInfo info = new EventInfo();
        info.key = key;
        info.handler = handler;
        if (EventInfoDict.ContainsKey(obj))
        {
            EventInfoDict[obj].Add(info);
        }
        else
        {
            List<EventInfo> list = new List<EventInfo>();
            list.Add(info);
            EventInfoDict.Add(obj, list);
        }
    }

    public static void RemoveListener(this object obj,object key,Action handler)
        {
            Instance.RemoveListener(key, handler);
        }

    public static void RemoveListener<T>(this object obj,object key,Action<T> handler)
        {
            Instance.RemoveListener(key, handler);
        }

    public static void RemoveListener<T, U>(this object obj,object key,Action<T,U> handler)
        {
            Instance.RemoveListener(key, handler);
        }

    public static void RemoveListener<T, U, V>(this object obj,object key,Action<T,U,V> handler)
        {
            Instance.RemoveListener(key, handler);
        }

    public static void RemoveListener<T, U, V, W>(this object obj,object key,Action<T,U,V,W> handler)
        {
            Instance.RemoveListener(key, handler);
        }

    public static void RemoveListener(this object obj)
    {
        if (EventInfoDict.ContainsKey(obj))
        {
            foreach (var item in EventInfoDict[obj])
            {
                Instance.RemoveListener(item.key, item.handler);
            }
        }
    }

    public static void Dispatch(this object obj,object key)
        {
            Instance.Dispatch(key);
        }

    public static void Dispatch<T>(this object obj,object key,T t)
        {
            Instance.Dispatch(key, t);
        }

    public static void Dispatch<T, U>(this object obj,object key ,T t,U u)
        {
            Instance.Dispatch(key, t, u);
        }

    public static void Dispatch<T, U, V>(this object obj,object key,T t,U u,V v)
        {
            Instance.Dispatch(key, t, u, v);
        }

    public static void Dispatch<T, U, V, W>(this object obj,object key,T t,U u,V v,W w)
        {
            Instance.Dispatch(key, t, u, v, w);
        }
    }
//}
